#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2025   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-GLJ2Q8J
 * 公司名称：
 * 命名空间：VSIXProjectGit.Config
 * 唯一标识：ac5a983e-2955-4e23-a623-068f5ee94183
 * 文件名：ConfigForm
 * 当前用户域：DESKTOP-GLJ2Q8J
 * 
 * 创建者： Guo_79991
 * 电子邮箱：799919859@qq.com
 * 创建时间：2025/9/25 13:03:35
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Windows.Forms;

namespace VSIXProjectGit.Config
{
    /// <summary>
    /// ConfigForm 的摘要说明
    /// </summary>

    // 简单的配置窗体
    public class ConfigForm : Form
    {
        private TextBox txtMessage;
        private NumericUpDown numInterval;
        private Button btnOk;
        private Button btnCancel;
        private Label lblWatermark;
        private ReminderConfig _config;
        private Label lblPreview;

        public ConfigForm(ReminderConfig config)
        {
            _config = config;
            InitializeComponent();
            UpdatePreview();
        }

        private void InitializeComponent()
        {
            // 窗体基本设置
            this.Text = "💧 喝水提醒配置 - YH Studio";
            this.Size = new Size(500, 400);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.BackColor = Color.FromArgb(240, 248, 255); // 淡蓝色背景
            this.Padding = new Padding(20);

            // 标题标签
            var lblTitle = new Label
            {
                Text = "喝水提醒配置",
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                ForeColor = Color.FromArgb(30, 144, 255),
                AutoSize = true,
                Location = new Point(20, 20)
            };

            // 提醒消息标签
            var lblMessage = new Label
            {
                Text = "提醒消息:",
                Font = new Font("微软雅黑", 10, FontStyle.Bold),
                ForeColor = Color.FromArgb(47, 79, 79),
                AutoSize = true,
                Location = new Point(20, 70)
            };

            // 提醒消息文本框
            txtMessage = new TextBox
            {
                Text = _config.Message,
                Location = new Point(120, 68),
                Size = new Size(340, 60),
                Multiline = true,
                ScrollBars = ScrollBars.Vertical,
                Font = new Font("微软雅黑", 9),
                BorderStyle = BorderStyle.FixedSingle,
                BackColor = Color.White
            };
            txtMessage.TextChanged += (s, e) => UpdatePreview();

            // 间隔时间标签
            var lblInterval = new Label
            {
                Text = "间隔时间:",
                Font = new Font("微软雅黑", 10, FontStyle.Bold),
                ForeColor = Color.FromArgb(47, 79, 79),
                AutoSize = true,
                Location = new Point(20, 140)
            };

            // 间隔时间数字选择框
            numInterval = new NumericUpDown
            {
                Value = _config.IntervalMinutes,
                Location = new Point(120, 138),
                Size = new Size(80, 25),
                Font = new Font("微软雅黑", 9),
                Minimum = 1,
                Maximum = 120,
                BorderStyle = BorderStyle.FixedSingle,
                BackColor = Color.White
            };
            numInterval.ValueChanged += (s, e) => UpdatePreview();

            var lblMinute = new Label
            {
                Text = "分钟",
                Font = new Font("微软雅黑", 9),
                ForeColor = Color.Gray,
                AutoSize = true,
                Location = new Point(210, 142)
            };

            // 预览区域
            var grpPreview = new GroupBox
            {
                Text = "预览效果",
                Font = new Font("微软雅黑", 9, FontStyle.Bold),
                Location = new Point(20, 180),
                Size = new Size(440, 80),
                BackColor = Color.White
            };

            lblPreview = new Label
            {
                Text = "",
                Font = new Font("微软雅黑", 9, FontStyle.Italic),
                ForeColor = Color.FromArgb(105, 105, 105),
                Location = new Point(10, 20),
                Size = new Size(420, 50),
                TextAlign = ContentAlignment.MiddleCenter
            };
            grpPreview.Controls.Add(lblPreview);

            // 水印标签
            lblWatermark = new Label
            {
                Text = "Copyright (C) YH Studio.",
                Font = new Font("微软雅黑", 8, FontStyle.Italic),
                ForeColor = Color.FromArgb(192, 192, 192),
                AutoSize = true,
                Location = new Point(20, 280)
            };

            // 确定按钮
            btnOk = new Button
            {
                Text = "✅ 确定",
                Font = new Font("微软雅黑", 10, FontStyle.Bold),
                ForeColor = Color.White,
                BackColor = Color.FromArgb(50, 205, 50), // 绿色
                FlatStyle = FlatStyle.Flat,
                Size = new Size(80, 35),
                Location = new Point(280, 270),
                Cursor = Cursors.Hand
            };
            btnOk.FlatAppearance.BorderSize = 0;
            btnOk.Click += BtnOk_Click;

            // 取消按钮
            btnCancel = new Button
            {
                Text = "❌ 取消",
                Font = new Font("微软雅黑", 10),
                ForeColor = Color.White,
                BackColor = Color.FromArgb(255, 69, 0), // 红色
                FlatStyle = FlatStyle.Flat,
                Size = new Size(80, 35),
                Location = new Point(390, 270),
                Cursor = Cursors.Hand
            };
            btnCancel.FlatAppearance.BorderSize = 0;
            btnCancel.Click += BtnCancel_Click;

            // 添加控件到窗体
            this.Controls.AddRange(new Control[]
            {
                lblTitle,
                lblMessage,
                txtMessage,
                lblInterval,
                numInterval,
                lblMinute,
                grpPreview,
                lblWatermark,
                btnOk,
                btnCancel
            });

            // 添加输入验证
            txtMessage.Validating += TxtMessage_Validating;
            numInterval.Validating += NumInterval_Validating;
        }

        private void UpdatePreview()
        {
            var interval = (int)numInterval.Value;
            var message = string.IsNullOrWhiteSpace(txtMessage.Text) ?
                "请输入提醒消息" : txtMessage.Text;

            lblPreview.Text = $"每隔 {interval} 分钟提醒：\n{message}";
        }

        private void TxtMessage_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtMessage.Text))
            {
                errorProvider.SetError(txtMessage, "提醒消息不能为空");
                btnOk.Enabled = false;
            }
            else
            {
                errorProvider.SetError(txtMessage, "");
                btnOk.Enabled = true;
            }
        }

        private void NumInterval_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (numInterval.Value < 1 || numInterval.Value > 120)
            {
                errorProvider.SetError(numInterval, "间隔时间必须在1-120分钟之间");
                btnOk.Enabled = false;
            }
            else
            {
                errorProvider.SetError(numInterval, "");
                btnOk.Enabled = true;
            }
        }

        private ErrorProvider errorProvider = new ErrorProvider();

        private void BtnOk_Click(object sender, EventArgs e)
        {
            // 验证输入
            if (string.IsNullOrWhiteSpace(txtMessage.Text))
            {
                System.Windows.Forms.MessageBox.Show("提醒消息不能为空", "输入错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (numInterval.Value < 1 || numInterval.Value > 120)
            {
                System.Windows.Forms.MessageBox.Show("间隔时间必须在1-120分钟之间", "输入错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        public ReminderConfig GetConfig()
        {
            return new ReminderConfig
            {
                Message = txtMessage.Text.Trim(),
                IntervalMinutes = (int)numInterval.Value,
                IsEnabled = _config.IsEnabled
            };
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 绘制边框
            using (var pen = new Pen(Color.FromArgb(176, 196, 222), 2))
            {
                e.Graphics.DrawRectangle(pen, 10, 10, this.Width - 30, this.Height - 50);
            }
        }
    }
}